<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/30
 * Time: 14:07
 */

class Group_manageController extends BaseController {
    public function index(){
        $doGroupDetail = new Do群详情();
        $doUser = new Do用户();
        $groupName = $this->request('groupName');
        if($groupName){
            $where =" a.群名称 like '%".$groupName."%' ";
        }
        $count = $doGroupDetail->where($where)->count();
        $page = new \tools\Page($count,15);
        $groups = $doGroupDetail->fields('a.*,b.name')
            ->leftJoin($doUser,'b','a.群主id=b.id')
            ->where($where)
            ->offset($page->limit)
            ->limit($page->pagesize)
            ->orderby('a.id desc')
            ->rows();
        foreach($groups as $k=>$v){
            $doGroupMember = new Do群成员();
            $doGroupMember->set_群id($v['id']);
            $num = $doGroupMember->fields('count(id)')->where($doGroupMember->data)->getOneField();
            $groups[$k]['num'] = $num;
        }
        require $this->template;
    }
//查看群成员
    public function view_group_members(){
        $doGroupMember = new Do群成员();
        $doUser = new Do用户();
        $groupId = $this->request('id','',true);
        $name = $this->request('name');
        $where = 'a.群id='.$groupId;
        if($name){
            $where.=" and b.name like '%".$name."%'";
        }
        $count = $doGroupMember->fields("count(a.id)")
            ->where($where)
            ->leftJoin($doUser,'b','a.用户id=b.id')->getOneField();
        $page = new \tools\Page($count,15);
        $doGroupMember = new Do群成员();
        $groupMembers = $doGroupMember->fields('a.*,b.name')
            ->where($where)
            ->leftJoin($doUser,'b','a.用户id=b.id')
            ->offset($page->limit)
            ->limit($page->pagesize)
            ->orderby('a.id desc')
            ->rows();
        require $this->template;
    }
    //删除群成员
    public function del()
    {
        $userId = $this->request('userId', '', true);
        $groupId = $this->request('groupId', '', true);
        $page = $this->request('page');
        $res = pdo()->delete('群成员',array('群id'=>$groupId,'用户id'=>$userId));
        $res1 = pdo()->delete('群聊',array('群id'=>$groupId,'用户id'=>$userId));
        if($res && $res1) {
            $url = $this->url('group_manage','view_group_members',array('id'=>$groupId,'page'=>$page));
            $this->successPage('删除成功',$url);
        }else{
            $this->errorPage('删除失败');
        }
    }
    //查看群聊记录
    public function view_chat_logs(){
        $doChat = new Do群聊();
        $doUser = new Do用户();
        $name = $this->request('name');
        $content = $this->request('content');
        $groupId = $this->request('id', '', true);
        $where = 'a.群id='.$groupId;
        if($name){
            $where.=" and b.name like '%".$name."%'";
        }
        if($content){
            $where.=" and a.聊天内容 like '%".$content."%'";
        }
        $count = $doChat->fields('count(a.id)')->leftJoin($doUser,'b','a.用户id=b.id')->where($where)->getOneField();
        $page = new \tools\Page($count,15);
        $doChat = new Do群聊();
        $chats = $doChat->fields('a.*,b.name')
            ->leftJoin($doUser,'b','a.用户id=b.id')
            ->where($where)
            ->offset($page->limit)
            ->limit($page->pagesize)
            ->orderby('a.id desc')
            ->rows();
        require $this->template;
    }
//删除聊天记录
    public function del_chat(){
        $chatId = $this->request('chatId', '', true);
        $groupId = $this->request('groupId', '', true);
        $doChat = new Do群聊();
        $doChat->set_id($chatId);
        $page = $this->request('page');
        $res = $doChat->delete();
        if($res) {
            $url = $this->url('group_manage','view_chat_logs',array('id'=>$groupId,'page'=>$page));
            $this->successPage('删除成功',$url);
        }else{
            $this->errorPage('删除失败');
        }
    }
    //导出群聊天内容
    public function export_chat(){
        require_once '../tools/excle/excel.class.php';
        $groupId = $this->request('groupId', '', true);
        $doChat = new Do群聊();
        $doUser = new Do用户();
        $chats = $doChat->fields('a.聊天内容, a.时间, b.name 姓名')
            ->leftJoin($doUser,'b','a.用户id=b.id')
            ->where("a.群id=".$groupId ." and a.聊天类型='文本'")
            ->orderby('a.id')
            ->rows();
        $xls= new excel('UTF-8', false, 'chats');
        $data[1]=array('姓名', '聊天内容', '时间');
        for ($i=0;$i<count($chats);$i++){
            $data[]=array("".$chats[$i]['姓名']."","".$chats[$i]['聊天内容']."","".$chats[$i]['时间']."");
        }
        $xls->addArray($data);
        $xls->generateXML('chats');
    }

}